<?php
App::uses('AppController', 'Controller');
App::uses('Xml', 'Utility');

class UpdateController extends AppController {
	public $name = 'Update';
	public $uses = null;
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('domain');

	}
	
	public function domain(){
		$this->render = false;
		
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		
// 		if(!$this->Domain->userAccess($this->Session->read('Auth.User'),'domain_id',$id,'DomainsUser')){
// 			throw new NotFoundException(__d('updates','You do not have permission to access this page'));
// 		}
		
		$updates = urldecode($this->request->data['updates']);		
		$updates = Xml::toArray(Xml::build($updates));	
			
		$this->log(print_r($updates,true));
		
		if(is_array($updates['update'])){
			
			foreach ($updates['update'] as $model => $value) {
				
				if($model != 'Menus') $this->loadModel($model);				
				$this->$model->recursive = -1;

				
				foreach ($value as $key => $a) {
					$this->$model->id = $a[$model]['id'];

					if(!$this->$model->save($a)){			
						$this->log(print_r($this->Recipe->validationErrors,true));
					}
				}
			}

		}
	}
	
}